package com.work.game.home.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.volley.Request;
import com.anythink.nativead.api.ATNativeAdView;
import com.gyf.immersionbar.ImmersionBar;
import com.work.game.R;
import com.work.game.ad.MediationBannerAd;
import com.work.game.ad.MediationFeedAd;
import com.work.game.ad.MediationTablePlaqueAd;
import com.work.game.ad.kaku.banner.TaKuBannerAd;
import com.work.game.ad.kaku.banner.TaKuBannerAdOBJ;
import com.work.game.ad.kaku.interstitia.TaKuInterstitialAdTwo;
import com.work.game.ad.kaku.nativie.TaKuNativeAdTwo;
import com.work.game.config.UrlConfig;
import com.work.game.home.ui.activity.CollectActivity;
import com.work.game.home.ui.activity.DeleteUserActivity;
import com.work.game.home.ui.activity.InviteRecordActivity;
import com.work.game.home.ui.activity.LikeGoodsActivity;
import com.work.game.home.ui.activity.RewardActivity;
import com.work.game.home.ui.activity.RewardRecordActivity;
import com.work.game.home.ui.activity.SetUpActivity;
import com.work.game.home.ui.activity.SignInActivity;
import com.work.game.home.ui.enums.MoneyTypeEnum;
import com.work.game.home.ui.enums.RedisEnum;
import com.work.game.home.ui.model.statistics.DayAdReward;
import com.work.game.home.ui.model.vo.BaseVo;
import com.work.game.home.ui.model.vo.UsersVo;
import com.work.game.utils.StringSplit;
import com.work.game.utils.risk.DeviceUtils;
import com.work.game.volley.BaseFragment;
import com.work.game.volley.state.HUCState;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;


public class MyFragmentStyle2 extends BaseFragment {
    private View view;

    private TextView tvMoneyRedPacket, tvMoneyGoldCoin, tvNickname, tvCode, tvCount;
    //update nickname
    private AlertDialog alertDialog;
    private EditText etName;
    private boolean isVisibleToUser = false;

    private MediationTablePlaqueAd mediationTablePlaqueAd;

    public MyFragmentStyle2() {
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        this.isVisibleToUser = isVisibleToUser;
        if (isVisibleToUser) {
            initAd();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (ObjectUtil.isNotNull(view)) {
            return view;
        }
        view = inflater.inflate(R.layout.view_page_my_style2, container, false);
        super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    public void onResume() {
        loadDayAdReward();
        ImmersionBar.with(this).titleBar(view.findViewById(R.id.layout_top)).statusBarDarkFont(true).init();
        super.onResume();
    }

    @Override
    public void initView() {
        super.initView();
        tvCount = view.findViewById(R.id.tv_count);
        tvMoneyGoldCoin = view.findViewById(R.id.tv_moneyGoldCoin);
        tvMoneyRedPacket = view.findViewById(R.id.tv_moneyRedPacket);
        tvNickname = view.findViewById(R.id.tv_nickname);
        tvCode = view.findViewById(R.id.tv_code);
        if(isVisibleToUser){
            initAd();
        }
        tvNickname.setText(getSharedPreferences("nickname"));
        tvCode.setText("ID:" + getSharedPreferences("id"));
    }

    @Override
    public void initClick() {
        super.initClick();
        view.findViewById(R.id.ll_setup).setOnClickListener(this);
        view.findViewById(R.id.ll_feedback).setOnClickListener(this);
        view.findViewById(R.id.tv_nickname).setOnClickListener(this);
//        view.findViewById(R.id.layout_sign).setOnClickListener(this);
        view.findViewById(R.id.layout_sign).setOnClickListener(this);
        view.findViewById(R.id.ll_invite_record).setOnClickListener(this);
        view.findViewById(R.id.tv_moneyRedPacket).setOnClickListener(this);
        view.findViewById(R.id.ll_delete).setOnClickListener(this);
        view.findViewById(R.id.layout_like).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_delete:
                sendIntent(this.getActivity(), DeleteUserActivity.class);
                break;
            case R.id.ll_setup:
                sendIntent(this.getActivity(), SetUpActivity.class);
                break;
            case R.id.ll_feedback:
                sendIntent(this.getActivity(), CollectActivity.class);
                break;
            case R.id.layout_like:
                sendIntent(this.getActivity(), LikeGoodsActivity.class);
                break;
//            case R.id.tv_reward_record:
//                sendIntent(this.getActivity(), RewardRecordActivity.class, String.valueOf(HUCState.HUC_REQEUST_CODE_1));
//                break;
//            case R.id.tv_h_bao:
//                sendIntent(this.getActivity(), RewardRecordActivity.class, String.valueOf(HUCState.HUC_REQEUST_CODE_2));
//                break;
            case R.id.ll_invite_record:
            case R.id.tv_moneyRedPacket:
                sendIntent(this.getActivity(), InviteRecordActivity.class);
                break;
            case R.id.layout_sign:
                sendIntent(this.getActivity(), SignInActivity.class, HUCState.HUC_REQEUST_CODE_1);
                break;
            case R.id.tv_nickname:
                showUpdateNickname();
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (!(response instanceof DayAdReward)) {
                    showToast(response.toString());
                    return;
                }
                DayAdReward dayAdReward = (DayAdReward) response;
                tvMoneyRedPacket.setText(dayAdReward.getMoney_red_envelope());
                tvMoneyGoldCoin.setText(dayAdReward.getMoney_gold_coins());
                String dayNumber = getSharedPreferences(RedisEnum.DAILY_TASK_FREQUENCY.getKey());
                tvCount.setText(dayAdReward.getDay_reward_count() + "/" + dayNumber);
                setSharedPreferences("day_reward_count", dayAdReward.getDay_reward_count());
                setSharedPreferences(MoneyTypeEnum.MONEY_COMMISSION_GOLD_COINS.getKey(), dayAdReward.getMoney_commission_gold_coins());
                setSharedPreferences(MoneyTypeEnum.MONEY_COMMISSION_RED_ENVELOPE.getKey(), dayAdReward.getMoney_commission_red_envelope());
                setSharedPreferences(MoneyTypeEnum.MONEY_EXTENSION_RED_ENVELOPE.getKey(), dayAdReward.getMoney_extension_red_envelope());
                setSharedPreferences(MoneyTypeEnum.MONEY_EXTENSION_RED_ENVELOPE.getKey(), dayAdReward.getMoney_extension_red_envelope());
                ProgressBar progressBar=view.findViewById(R.id.progress_bar);
                try {
                   double rewardCountTask= Double.valueOf(dayAdReward.getDay_reward_count());
                   double dayNumberAll= Double.valueOf(dayNumber);
                   if(rewardCountTask>0){
                       int progressValue= (int) (rewardCountTask/dayNumberAll*100);
                       progressBar.setProgress(progressValue);
                   }else{
                       progressBar.setProgress(0);
                   }
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                if (response instanceof String) {
                    String msg = (String) response;
                    showToast(msg);
                    alertDialog.dismiss();
                    setSharedPreferences("nickname", etName.getText().toString());
                    tvNickname.setText(etName.getText().toString());
                }
                break;
        }
        loadDismiss();
    }

    private void loadDayAdReward() {
        //获取网络数据
        loading();
        BaseVo baseVo = new BaseVo();
        baseVo.setToken(getSharedPreferences("deviceId"));
        baseVo.setOaid(DeviceUtils.getInstance().getOaId(this.getActivity()));
        hucRequestInterfase.executeStr(this, DayAdReward.class, HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_TO_DAY_STATISTICS, hucParameInterfase.getRequestParameter(baseVo), true);
    }

    private void showUpdateNickname() {
        //判断提现账号
        AlertDialog.Builder dialog = new AlertDialog.Builder(this.getActivity());
        View root = LayoutInflater.from(this.getActivity()).inflate(R.layout.pop_update_nickname, null);
        dialog.setView(root);
        etName = root.findViewById(R.id.et_nickname);
        etName.setText(getSharedPreferences("nickname"));
        root.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = etName.getText().toString();
                if (StrUtil.isEmpty(name)) {
                    showToast("用户名不能为空");
                    return;
                }

                if (StrUtil.length(name) > 6) {
                    showToast("用户名最多六个字符");
                    return;
                }
                UsersVo usersVo = new UsersVo();
                usersVo.setToken(getSharedPreferences("deviceId"));
                usersVo.setNickname(name);
                //请求后台接口
                hucRequestInterfase.executeStr(MyFragmentStyle2.this, null, HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.URL_UPDATE_NICKNAME, hucParameInterfase.getRequestParameter(usersVo), true);
            }
        });
        root.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog = dialog.create();
        alertDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        alertDialog.setCancelable(false);
        alertDialog.show();
    }

    private void initAd() {
        if (ObjectUtil.isNull(view)) {
            return;
        }
        ATNativeAdView adTop = view.findViewById(R.id.ad_top);
        ATNativeAdView adBottom = view.findViewById(R.id.ad_bottom);
        String key = getSharedPreferences(RedisEnum.TA_KU.getKey());
        ATNativeAdView flBanner = view.findViewById(R.id.fl_banner);

        if (RedisEnum.TA_KU.getValue().equals(key)) {
            String code1=StringSplit.splitOrNull(getSharedPreferences(RedisEnum.BANNER.getKey()), StrUtil.COMMA, 4);
            String code2=StringSplit.splitOrNull(getSharedPreferences(RedisEnum.BANNER.getKey()), StrUtil.COMMA, 5);
            TaKuInterstitialAdTwo taKuInterstitialAdTwo = TaKuInterstitialAdTwo.getInstance();
            taKuInterstitialAdTwo.setToken(getSharedPreferences("deviceId"));
            taKuInterstitialAdTwo.showAd(this.getActivity());

            TaKuBannerAdOBJ taKuBannerAd1 = new TaKuBannerAdOBJ();
            taKuBannerAd1.setToken(getSharedPreferences("deviceId"));
            taKuBannerAd1.load(getContext(),code1, adTop);

            TaKuBannerAdOBJ taKuBannerAd2 = new TaKuBannerAdOBJ();
            taKuBannerAd2.setToken(getSharedPreferences("deviceId"));
            taKuBannerAd2.load(getContext(),code2, adBottom);


        } else {
            //插屏广告
            MediationTablePlaqueAd mediationTablePlaqueAd = MediationTablePlaqueAd.getInstance();
            mediationTablePlaqueAd.showCacheInterstitialFullAd(this.getActivity(), null);


            String code1 = StringSplit.splitOrNull(getSharedPreferences(RedisEnum.AD_INFORMATION_FLOW.getKey()), StrUtil.COMMA, 4);
            String code2 =StringSplit.splitOrNull(getSharedPreferences(RedisEnum.AD_INFORMATION_FLOW.getKey()),StrUtil.COMMA,5);
            MediationFeedAd mediationFeedAdTop = new MediationFeedAd();
            mediationFeedAdTop.init(code1);
            mediationFeedAdTop.setFlFeed(adTop);
            mediationFeedAdTop.setActivity(this.getActivity());
            mediationFeedAdTop.showCacheFeedAd();


            MediationFeedAd mediationFeedAdBottom = new MediationFeedAd();
            mediationFeedAdBottom.init(code2);
            mediationFeedAdBottom.setFlFeed(adBottom);
            mediationFeedAdBottom.setActivity(this.getActivity());
            mediationFeedAdBottom.showCacheFeedAd();
        }


    }

}
